package cn.edu.scnu.controller;

import cn.edu.scnu.entity.Movie;
import cn.edu.scnu.service.GradeService;
import cn.edu.scnu.service.MovieService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.List;

@Controller
public class ExcelController {
    @Autowired
    private GradeService gradeService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/getExcelFile")
    public void getExcelFile(HttpServletResponse response) throws IOException {
        // 获取电影榜单数据
        List<Movie> movieList = (List<Movie>)redisTemplate.opsForValue().get("movieGrade");

        // 创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 创建工作表
        XSSFSheet sheet = workbook.createSheet("电影榜单");
        // 标题行样式
        XSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 创建标题行
        XSSFRow row = sheet.createRow(0);

        XSSFCell c0 = row.createCell(0);
        c0.setCellValue("名称");
        c0.setCellStyle(headerStyle);
        XSSFCell c1 = row.createCell(1);
        c1.setCellValue("类型");
        c1.setCellStyle(headerStyle);
        XSSFCell c2 = row.createCell(2);
        c2.setCellValue("地区");
        c2.setCellStyle(headerStyle);
        XSSFCell c3 = row.createCell(3);
        c3.setCellValue("简介");
        c3.setCellStyle(headerStyle);
        XSSFCell c4 = row.createCell(4);
        c4.setCellValue("评分");
        c4.setCellStyle(headerStyle);
        XSSFCell c5 = row.createCell(5);
        c5.setCellValue("播放量");
        c5.setCellStyle(headerStyle);

        // 在表中写入数据
        int rowNum = 1;
        for (Movie movie : movieList) {
            XSSFRow addRow = sheet.createRow(rowNum++);
            addRow.createCell(0).setCellValue(movie.getMovieName());
            addRow.createCell(1).setCellValue(movie.getMovieType());
            addRow.createCell(2).setCellValue(movie.getMovieArea());
            addRow.createCell(3).setCellValue(movie.getMovieIntro());
            addRow.createCell(4).setCellValue(movie.getScore());
            addRow.createCell(5).setCellValue(movie.getPlays());
        }

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=movie_grade.xlsx");
        workbook.write(response.getOutputStream());
        workbook.close();;
    }

}
